import __config from 'config/env';
import util from 'utils/util';

const request = (url, method, data, showLoading, tenantId, dataType) => {
	let _url = url;
	_url = 'https://mall.ljcds.cn' + url

	return new Promise((resolve, reject) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		let header = {
			'client-type': 'MA', //客户端类型小程序
			'tenant-id': 1,
			'app-id': 'wxa0e70c974a1c9bc7', //小程序appId
			'third-session': '4132332c-21e8-462c-b34c-2d1ade489daf',
		}
	
		uni.request({
			url: _url,
			method: method,
			data: data,
			withCredentials: true,
			header: header,
			success(res) {
				if (res.statusCode == 200) {
					if (res.data.code != 0) {
						if (res.data.code == 60001) {
							//session过期，则清除过期session，并重新加载当前页
							if (!getApp().globalData.logining) {
								getApp().globalData.logining = true //防止同时多个接口触发登录
								uni.removeStorageSync('third_session');
								setTimeout(function() {
									getApp().globalData.logining = false;
								}, 5000);
								getApp().initPage().then(res => {
									var pages = getCurrentPages(); // 获取页面栈
									var currPage = pages[pages.length - 1]; // 当前页面
									if (currPage) {
										currPage.onLoad(currPage.options)
										currPage.onShow()
									}
								});
							}
							reject("session过期重新登录");
						} else if (res.data.code == 60002 || res.data.code == 60003) {
							uni.removeStorageSync('third_session');
							uni.removeStorageSync('user_info')
							uni.reLaunch({
								url: '/pages/home/index'
							});
							resolve(res.data);
							// const flag = uni.getStorageSync('reLoadLoading')
							// console.log(res.data)
							// if (res.data.data) {
							// 	uni.setStorageSync('third_session', res.data.data.thirdSession);
							// }
							// if (!flag) {
							// 	var pages = getCurrentPages(); // 获取页面栈
							// 	var currPage = pages[pages.length - 1]; // 当前页面								
							// 	if (currPage) {
							// 		let reUrl = '/' + currPage.route
							// 		uni.setStorageSync('reUrl', currPage.$page.fullPath);

							// 	}
							// }
							// reject("请先登录商城");
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.msg + '',
								success() {},
								complete() {

								}
							});
							reject(res.data.msg);
						}
					}
					resolve(res.data);
				} else if (res.statusCode == 404) {
					uni.showModal({
						title: '提示',
						content: '找不到服务端，请联系客服',
						success(res) {}
					});
					reject();
				} else {
					console.log(res);
					// uni.showModal({
					// 	title: '提示',
					// 	content: '接口请求出错，请检查手机网络',
					// 	success(res) {}
					// });
					reject(res);
				}
			},
			fail(error) {
				console.log(error, _url, 23232222);
				// uni.showModal({
				// 	title: '提示',
				// 	content: '接口请求出错，请检查手机网络：',
				// 	success(res) {}
				// });
				reject(error);
			},
			complete(res) {
				uni.hideLoading();
			}
		});
	});
};

module.exports = {
	request,
	// 微信登录
	loginWxMa: data => {
		//如果有分享人则带上分享人的user_code
		data = util.dataAddSharerUserCode(data)
		//微信小程序登录接口
		return request('/mallapi/wxuser/loginma', 'post', data, true);
	},
	userInfoGet: () => {
		//查询商城用户信息
		return request('/mallapi/userinfo', 'get', null, true);
	},
	// 消息列表
	messagePage: data => {
		return request('/mallapi/message/page', 'get', data, true);
	},
	// 消息详情
	messageGet: id => {
		return request('/mallapi/message/queryContentById?id=' + id, 'get', null, true);
	},
	// 物料列表
	materialPage: data => {
		return request('/mallapi/material/page', 'get', data, true);
	},
	// 物料详情
	materialGet: id => {
		return request('/mallapi/material/queryContentById?id=' + id, 'get', null, true);
	},
	//用户收藏新增	
	userCollectAdd: data => {
		return request('/mallapi/usercollect', 'post', data, true);
	},
	//用户收藏删除	
	userCollectDel: data => {
		return request('/mallapi/usercollect/delete', 'get', data, true);
	},
	//用户收藏列表	
	userCollectPage: data => {
		return request('/mallapi/usercollect/page', 'get', data, true);
	},
	// 判断单个商品是否被收藏
	userCollectCheck: data => {
		return request('/mallapi/usercollect/judge', 'get', data, true);
	},
	// 获取榜单 -- 分类
	getRankType: data => {
		return request('/mallapi/rankingType/queryAll', 'get', null, true);
	},
	// 获取榜单 -- 榜单
	getRankList: data => {
		return request('/mallapi/ranking/queryByTypeId', 'get', data, true);
	},
	// 查询 -- 榜单商品
	getRankingGoods: data => {
		return request('/mallapi/rankingGoods/query', 'get', data, true);
	},
	//库存查询	
	getGoodsStock: (skuId) => {
		return request('/mallapi/goodssku/getStock/' + skuId, 'get', null, true);
	},
	//订单列表	
	orderPage: data => {
		return request('/mallapi/orderinfo/page1', 'get', data, true);
	},
	// 	埋点 -- 生成分享记录	
	shareInfo: (data) => {
		return request('/mallapi/shareinfo', 'post', data, true);
	},
	// 查询 -- 分享统计数据
	getStatistics: (days, userId) => {
		return request('/mallapi/shareinfo/statistics?days=' + days + '&userId=' + userId, 'get', null, true);
	},
	//佣金明细	
	getCommissionById: id => {
		return request('/mallapi/usercommission/' + id, 'get', null, true);
	},
	// 提现记录
	getExtractRecord: data => {
		return request('/mallapi/commission_record/page', 'get', data, true);
	},
	// 申请提现
	userwithdrawRecordSave: data => {
		return request('/mallapi/commission_record', 'post', data, true);
	},
	getDeliveryInfo: (id) => {
		// 	订单物流查询 -- 快100
		return request('/mallapi/kuaidi100/getDeliveryInfo/' + id, 'get', null, true, true);
	},
	getByBarcode: (barcode) => {
		// 	根据UPC条形码条形码，查询商品
		return request('/mallapi/goodsspu/getByBarcode/' + barcode, 'get', null, true, true);
	},
	changeMobile: data => {
		// 	修改手机号
		return request('/mallapi/userinfo/changeMobile', 'post', data, true);
	},




	loginWxMp: data => {
		//微信公众号登录接口
		return request('/mallapi/wxuser/loginmp', 'post', data, true);
	},


	loginByPhoneMa: data => {
		//微信商城通过小程序授权手机号一键登录商城
		return request('/mallapi/userinfo/ma/phone/login', 'post', data, true);
	},

	loginByPhone: data => {
		//商城手机验证码登录商城
		return request('/mallapi/userinfo/phone/login', 'post', data, true);
	},
	loginByPhone0: data => {
		//商城手机号登录商城
		return request('/mallapi/userinfo/phone/login0', 'post', data, true);
	},
	login: data => {
		//商城账号登录
		return request('/mallapi/userinfo/', 'post', data, true);
	},
	logout: data => {
		//商城退出登录
		return request('/mallapi/userinfo/logout', 'post', data, true);
	},
	getPhoneCode: data => {
		//获取手机验证码
		return request('/mallapi/phone/code', 'get', data, true);
	},
	register: data => {
		//账号注册
		return request('/mallapi/userinfo/register', 'post', data, true);
	},

	themeMobileGet: () => {
		//获取商城主题装修配置
		return request('/mallapi/thememobile', 'get', null, true);
	},
	shopInfoPage: data => {
		//店铺列表
		return request('/mallapi/shopinfo/page', 'get', data, true);
	},
	shopInfoPageWithSpu: data => {
		//店铺列表带商品
		return request('/mallapi/shopinfo/pagewithspu', 'get', data, true);
	},
	shopInfoGet: id => {
		//店铺查询
		return request('/mallapi/shopinfo/' + id, 'get', null, true);
	},
	shopInfoGetByShopCode: code => {
		//店铺查询
		return request('/mallapi/shopinfo/code/' + code, 'get', null, true);
	},
	userInfoUpdateByMa: data => {
		//通过微信小程序更新用户信息
		return request('/mallapi/userinfo/ma', 'put', data, true);
	},
	userInfoUpdateByMp: data => {
		//通过微信公众号网页授权更新用户信息
		return request('/mallapi/userinfo/mp', 'put', data, true);
	},
	goodsCategoryTree: data => {
		//商城商品分类tree查询
		return request('/mallapi/goodscategory/tree', 'get', data, true);
	},
	getHomepageGoods: data => {
		//首页/二级首页装修  获取商品列表信息(id集合)
		return request('/mallapi/goodssku/goodsspu/list', 'post', data, true, true, true);
	},
	goodsCategoryPage: data => {
		//商城商品分类list查询
		return request('/mallapi/goodscategory/page', 'get', data, true);
	},

	goodsPage: data => {
		//商品列表
		return request('/mallapi/goodsspu/page', 'get', data, true);
	},
	goodsPage3: data => {
		//券跳转商品列表
		return request('/mallapi/goodsspu/page3', 'get', data, true);
	},
	goodsGetByCode: spuCode => {
		//商品查询
		return request('/mallapi/goodsspu/spucode/' + spuCode, 'get', null, true);
	},
	goodsGet: (id, activityId) => {
		//商品查询		
		if (activityId) {
			return request('/mallapi/goodsspu/' + id + '/' + activityId, 'get', null, true);
		} else {
			return request('/mallapi/goodsspu/' + id, 'get', null, true);
		}
	},

	getByLocationAndSpuId: (data) => {
		//库存查询
		return request('/mallapi/goodsstock/location', 'get', data, true);
	},
	goodsSpecGet: data => {
		//商品规格查询
		return request('/mallapi/goodsspuspec/tree', 'get', data, true);
	},

	orderSub: data => {
		//订单提交
		return request('/mallapi/orderinfo', 'post', data, true);
	},


	orderGet: id => {
		//订单详情查询
		return request('/mallapi/orderinfo/' + id, 'get', null, true);
	},

	orderCountAll: data => {
		//订单计数
		return request('/mallapi/orderinfo/countAll', 'get', data, true);
	},
	orderLogisticsGet: logisticsId => {
		//订单物流查询
		return request('/mallapi/orderinfo/orderlogistics/' + logisticsId, 'get', null, true);
	},
	unifiedOrder: data => {
		//下单接口
		return request('/mallapi/orderinfo/unifiedOrder', 'post', data, true);
	},


	qrCodeUnlimited: data => {
		//获取小程序二维码 -- 微信
		return request('/mallapi/wxqrcode/unlimited', 'post', data, true);
	},


	userInfoGet: () => {
		//查询商城用户信息
		return request('/mallapi/userinfo', 'get', null, true);
	},

	pointsRecordPage: data => {
		//查询积分记录
		return request('/mallapi/pointsrecord/page', 'get', data, true);
	},
	pointsConfigGet: () => {
		//查询积分配置
		return request('/mallapi/pointsconfig', 'get', null, true);
	},



	listEnsureBySpuId: data => {
		//通过spuID，查询商品保障
		return request('/mallapi/ensuregoods/listEnsureBySpuId', 'get', data, true);
	},



	wxTemplateMsgList: data => {
		//订阅消息列表
		return request('/mallapi/wxtemplatemsg/list', 'post', data, false);
	},

	customerServiceList: shopId => {
		//客服列表
		return request('/mallapi/customerservice/list/' + shopId, 'get', null, false);
	},
	pagedevise: data => {
		//首页组件配置
		return request('/mallapi/pagedevise', 'get', data, null, true);
	},
	pagedeviseShop: shopId => {
		// 店铺组件配置
		return request('/mallapi/pagedevise?pageType=2&shopId=' + shopId, 'get', null, true);
	},


	articleCategoryPage: data => {
		//文章分类列表
		return request('/mallapi/articlecategory/page', 'get', data, true);
	},
	userSign: data => {
		//积分签到
		return request('/mallapi/signrecord/user', 'post', data, true);
	},
	getSignRecord: data => {
		//签到记录查询
		return request('/mallapi/signrecord/user', 'get', data, true);
	},
	signConfigPage: data => {
		//获取签到积分记录
		return request('/mallapi/signconfig/page', 'get', data, true);
	},
	getShopsList: data => {
		//获取不同门店
		return request('/mallapi/shopinfo/mapList', 'get', data, true);
	},
	getSwitchShop: data => {
		//获取不同门店
		return request('/mallapi/shopinfo/switchShop', 'get', data, true);
	},





	getCommission: data => {
		//推广数据接口
		return request('/mallapi/usercenter/commission', 'get', data, true);
	},

	getSwitchBargainshop: data => {
		//获取不同门店
		return request('/mallapi/shopinfo/switchBargainshop', 'get', data, true);
	},
	orderReckonCoupon: data => {
		// 使用券后，计算折扣金额
		return request('/mallapi/orderinfo/reckonCoupon', 'post', data, true);
	},
	distributionConfig: () => {
		//分销设置查询
		return request('/mallapi/distributionconfig', 'get', null, true);
	},
	distributionuser: () => {
		//分销员查询
		return request('/mallapi/distributionuser', 'get', null, true);
	},
	distributionuserPage: (data) => {
		//分销员查询
		return request('/mallapi/distributionuser/page', 'get', data, true);
	},
	distributionuserPage2: (data) => {
		// 推广下级查询
		return request('/mallapi/distributionuser/page2', 'get', data, true);
	},
	distributionorderPage: data => {
		//分销订单分页
		return request('/mallapi/distributionorder/page', 'get', data, true);
	},
	distributionorderFrozenAmount: data => {
		//获取当前分销员的冻结金额
		return request('/mallapi/distributionorder/frozenAmount', 'get', data, true);
	},
	distributionPromotionPage: data => {
		//分销 推广人统计
		return request('/mallapi/userinfo/page', 'get', data, true);
	},

	userwithdrawRecordUpdate: data => {
		//分销 提现申请修改
		return request('/mallapi/userwithdrawrecord', 'put', data, true);
	},
	userwithdrawRecordPage: data => {
		//分销 提现记录
		return request('/mallapi/userwithdrawrecord/page', 'get', data, true);
	},
	userwithdrawRecord: id => {
		//分销 提现记录详情
		return request('/mallapi/userwithdrawrecord/' + id, 'get', null, true);
	},
	getCheckAmount: () => {
		//分销 提现记录详情
		return request('/mallapi/userwithdrawrecord/getCheckAmount', 'get', null, true);
	},
	userRecord: () => {
		//分销 用户消费总额记录
		return request('/mallapi/userrecord', 'get', null, true);
	},
	getTradeno: (id) => {
		//获取医保订单号
		return request('/mallapi/yb/submit?orderId=' + id, 'get', null, true, true);
	},
	savetoken: data => {
		//保存医保token
		return request('/mallapi/orderinfo/savetoken', 'get', data, true);
	},
	gettoken: () => {
		//获取医保token
		return request('/mallapi/orderinfo/gettoken', 'get', null, true);
	},
	sendmsg: data => {
		//发送消息
		return request('/mallapi/kf/sendmsg', 'get', data, false, true);
	},
	check: data => {
		//支付完成，查询是否需要开方
		return request('/mallapi/kf/check' + data, 'get', null, false, true);
	},
	getDiseases: data => {
		//处方药  完善用户信息  病症选项
		return request('/mallapi/kf/getGoodsDiseases?goodsid=' + data, 'get', data, true, true);
	},
	kfres: data => {
		//处方药  完善用户信息  病症选项
		return request('/mallapi/kf/kfres?orderId=' + data, 'get', data, false, true);
	},
	MakePT: data => {
		//处方药  开方
		return request('/mallapi/orderinfo/kf/' + data, 'get', null, false, true);
	},
	bindzz: data => {
		//中智 token换用户信息
		return request('/mallapi/userinfo/bindzz?token=' + data, 'post', null, false, true);
	},
	notifyZzOrder: data => {
		//中智 验证手机号
		return request('/mallapi/orderinfo/notify-zzorder', 'post', data, true, true);
	},
	getActiveList: (id) => {
		//获取活动装修列表
		return request('/mallapi/activitypage/' + id, 'get', null, true, true);
	},
	getActiveGoods: (data) => {
		//获取活动商品列表
		return request('/mallapi/activitygoods/page', 'get', data, true, true);
	},
	getActiveGoodByid: data => {
		// 根据商品id(多个) -- 获取商品信息
		return request('/mallapi/activitygoods/list', 'post', data, true, true, true);
	},
	getActiveByid: id => {
		// 根据活动id -- 查询活动信息
		return request('/mallapi/activity/' + id, 'get', null, true, true, true);
	},
	getactivityCoupon: (data) => {
		//根据活动id查询活动券
		return request('/mallapi/activitypage/activity_coupon', 'get', data, true, true);
	},
	epidemicCheck: (data) => {
		// 提交订单前 -- 判断是否是四类药
		return request('/mallapi/orderinfo/epidemicCheck', 'post', data, true, true);
	},


	signpageconfig: () => {
		// 获取签到配置
		return request('/mallapi/signpageconfig', 'get', null, true, true);
	},
	getCenterShop: (type) => {
		// 获取总仓 -- 医药/母婴
		return request('/mallapi/shopinfo/getCenterShop/' + type, 'get', true, true);
	},
	getAllSpuId: (id) => {
		// 	获取全部spuId
		return request('/mallapi/goodsspu/getById13/' + id, 'get', true, true);
	},
	getAllactivId: (id) => {
		// 	获取全部活动ID
		return request('/mallapi/goodsspu/getActById13/' + id, 'get', true, true);
	},
	checkGoodsspuQuota: (data) => {
		// 	商品购买或加入购物车前 -- 判断商品限购
		return request('/mallapi/orderinfo/checkGoodsspuQuota', 'post', data, true, true);
	},

	getBuyCounts: (id) => {
		// 	积分商品 -- 当月购买数量查询
		return request('/mallapi/orderinfo/getBuyCounts?spuid=' + id, 'get', null, true, true);
	},

	shareUserFoot: (data) => {
		// 	埋点 -- 用户足迹
		return request('/mallapi/shareUserFoot', 'post', data, true);
	},
	getPosterId: (data) => {
		// 	商品详情页面 -- 生成分享页面携带参数Id
		return request('/mallapi/usercenter/getposterId', 'post', data, true);
	},
	getPoster: (id) => {
		// 	商品详情页面 -- 获取分享页面携带参数
		return request('/mallapi/usercenter/getposter?id=' + id, 'get', null, true);
	},
	getHomeFloat: () => {
		// 	首页浮窗
		return request('/mallapi/floatimage/get', 'get', null, true, true);
	},
	updateMaNick: (data) => {
		// 	更新昵称、头像
		return request('/mallapi/userinfo/maNick', 'post', data, true);
	},
	wxTextCheck: (content) => {
		// 	敏感信息检测
		return request('/weixin/wxmassmsg/textCheck?content=' + content, 'get', null, true, true);
	},
	wxImgCheck: (url) => {
		// 	敏感图片检测
		return request('/weixin/wxmassmsg/imgCheck?url=' + url, 'get', null, true, true);
	},
};